package com.ys.util.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.bo.dto.NameValueSelectDto;
import com.ys.entity.sys.YsDict;
import com.ys.entity.sys.YsModalSelectorField;
import com.ys.enume.sys.YsModalSelectorFieldEnum;
import com.ys.service.sys.YsModalSelectorService.ModalConfigDto;

public class GridSelectorModalUtil {
	
	/*
	 * 获取bootstrap table的columns
	 */
	public static List<ModalConfigDto.ColGridDto> listColumn(List<YsModalSelectorField> listYsModalSelectorField, Integer multySelected, Map<String, List<YsDict>> KEY_LISTDICT_MAP) {
		List<ModalConfigDto.ColGridDto> colGridArr = new ArrayList<ModalConfigDto.ColGridDto>();
		
		// 选中框
		if (multySelected == 0) {
			// 单选框
			String title = "";
			String field = null;
			String type = "radio";
			String fixed = null;
			boolean unresize = true;
			String width = "50";
			String align = null;
			String templet = null;
			String baseDictKey = null;
			
			colGridArr.add(new ModalConfigDto.ColGridDto(title, field, type, fixed, unresize, width, align, templet, baseDictKey));
		} else {
			// 复选框
			String title = "";
			String field = null;
			String type = "checkbox";
			String fixed = null;
			boolean unresize = true;
			String width = "50";
			String align = null;
			String templet = null;
			String baseDictKey = null;
			
			colGridArr.add(new ModalConfigDto.ColGridDto(title, field, type, fixed, unresize, width, align, templet, baseDictKey));
		}
		
		{
			String title = "序号";
			String field = null;
			String type = "numbers";
			String fixed = null;
			boolean unresize = true;
			String width = "60";
			String align = "center";
			String templet = null;
			String baseDictKey = null;
			
			colGridArr.add(new ModalConfigDto.ColGridDto(title, field, type, fixed, unresize, width, align, templet, baseDictKey));
		}
		
		for (YsModalSelectorField ysModalSelectorField : listYsModalSelectorField) {
			
			String title = ysModalSelectorField.getName();
			String field = ysModalSelectorField.getCode();
			String type = null;
			String fixed = null;
			boolean unresize = false;
			String width = ysModalSelectorField.getWidth().toString();
			String align = YsModalSelectorFieldEnum.Align.getName(ysModalSelectorField.getAlign());
			String templet = null;
			String baseDictKey = null;
			
			if (YsModalSelectorFieldEnum.Type.SELECTOR_DROPDOWN.value.equals(ysModalSelectorField.getType())
					|| YsModalSelectorFieldEnum.Type.SELECTOR_SWITCH.value.equals(ysModalSelectorField.getType())) {
				templet = "#templet_script_tpl_for_modal_" + ysModalSelectorField.getCode(); // 此处的templet格式需与前端保持一致
				baseDictKey = ysModalSelectorField.getDictKeyBase();
			}
			
			colGridArr.add(new ModalConfigDto.ColGridDto(title, field, type, fixed, unresize, width, align, templet, baseDictKey));
		}
		return colGridArr;
	}
	
	/*
	 * 获取搜索的输入框
	 */
	public static List<ModalConfigDto.ConditionDto> listCondition(List<YsModalSelectorField> listYsModalSelectorField, Map<String, List<YsDict>> KEY_LISTDICT_MAP) {
		List<ModalConfigDto.ConditionDto> listCondition = new ArrayList<ModalConfigDto.ConditionDto>();
		for (YsModalSelectorField tsDictionaryForField : listYsModalSelectorField) {
			ModalConfigDto.ConditionDto conditionDto = getCondition(tsDictionaryForField);
			if (conditionDto != null) {
				listCondition.add(conditionDto);
			}
		}
		
		return listCondition;
	}
	
	/*
	 * 获取搜索的输入框
	 */
	public static ModalConfigDto.ConditionDto getCondition(YsModalSelectorField ysModalSelectorField) {
		
		if (YsModalSelectorFieldEnum.StateCondition.NO.value == ysModalSelectorField.getStateCondition()) {
			return null;
		}
		
		Long id = ysModalSelectorField.getId();
		String name = ysModalSelectorField.getName();
		String code = ysModalSelectorField.getConditionCode() != null && ysModalSelectorField.getConditionCode().length() > 0 ? ysModalSelectorField.getConditionCode() : ysModalSelectorField.getCode();
		String type = ysModalSelectorField.getType();
		String rangeType = "t_bothlike";
		String value = null;
		String placeHolder = "模糊匹配";
		String dictKeyBase = ysModalSelectorField.getDictKeyBase();
		
		if (YsModalSelectorFieldEnum.Type.TEXT.value.equals(type)) {
			return new ModalConfigDto.ConditionDto(id, code, type, rangeType, name, value, placeHolder, dictKeyBase, null);
		} else if (YsModalSelectorFieldEnum.Type.SELECTOR_DROPDOWN.value.equals(type)) {
			List<NameValueSelectDto> optionArr = getOptionArr(ysModalSelectorField.getDictKeyBase(), 1);
			return new ModalConfigDto.ConditionDto(id, code, type, rangeType, name, value, placeHolder, dictKeyBase, optionArr);
		} else if (YsModalSelectorFieldEnum.Type.SELECTOR_SWITCH.value.equals(type)) {
			List<NameValueSelectDto> optionArr = getOptionArr(ysModalSelectorField.getDictKeyBase(), 0);
			return new ModalConfigDto.ConditionDto(id, code, type, rangeType, name, value, placeHolder, dictKeyBase, optionArr);
		} else if (YsModalSelectorFieldEnum.Type.DATE.value.equals(type)) {
			placeHolder = "请选择" + name;
			return new ModalConfigDto.ConditionDto(id, code, type, rangeType, name, value, placeHolder, dictKeyBase, null);
		} else {
			return null;
		}
	}
	
	public static List<NameValueSelectDto> getOptionArr(String dictKey, Integer typeAddOption) {
		if (dictKey != null && dictKey.length() > 0) {
			List<YsDict> listYsDict = new YsDict().setDictKey(dictKey).queryList();
			List<NameValueSelectDto> optionArr = new ArrayList<NameValueSelectDto>();
			if(typeAddOption == 1) {
				String name = "请选择";
				String value = "";
				optionArr.add(new NameValueSelectDto(name, value, false));
			}
			for(YsDict ysDict : listYsDict) {
				String name = ysDict.getDictName();
				String value = ysDict.getDictValue();
				optionArr.add(new NameValueSelectDto(name, value, false));
			}
			return optionArr;
		} else {
			return null;
		}
	}
	
//	public static String ColGridToString(List<ModalConfigDto.ColGridDto> colGridArr) {
//		StringBuffer sb = new StringBuffer("[");
//		for (int i = 0; colGridArr != null && i < colGridArr.size(); i++) {
//			ModalConfigDto.ColGridDto colGridDto = colGridArr.get(i);
//			
//			if (i == 0) {
//				sb.append(colGridDto.toString());
//			} else {
//				sb.append("," + colGridDto.toString());
//			}
//		}
//		sb.append("]");
//		return sb.toString();
//	}
}
